package com.vulb.logic;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.vulb.application.GlobalContext;
import com.vulb.bean.Session;
import com.vulb.db.DbHelper;

public class SessionManager {
    private static SessionManager instance = new SessionManager();
    private Session m_current_session;
    private DbHelper db = null;
    private Dao<Session, Integer> mSessionDao;
    private SharedPreferences m_current_logged_user = null;
    public static final String logged_user_info = "user info";
	public static final String current_user_account = "user account";
	public static final String current_user_password = "user password";
   
    public SessionManager()
    {
    	mSessionDao = DbHelper.getInstance().getSessionDao(Session.class);
    	m_current_logged_user = GlobalContext.getInstance().getContext().getSharedPreferences(logged_user_info,0);
    }
    public static SessionManager getInstance() 
    {
        return instance;
    }
    public Session getCurrentSession()
    {
    	return this.m_current_session;
    }
    public void login(Session s)
    {
    	this.m_current_session = s;
    	Editor editor = m_current_logged_user.edit();
		editor.putString(current_user_account,s.getAccont());
		editor.putString(current_user_password,s.getpwd());
		editor.commit();
    	try 
    	{
			mSessionDao.createOrUpdate(s);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public List<Session> getAllSessions()
    {
    	try 
    	{
			return this.mSessionDao.queryForAll();
		} catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
    }
    //TODO
    
}